package com.kgc.scd.controller;

import com.kgc.scd.annotation.RequestShopUser;
import com.kgc.scd.annotation.TokenPermission;
import com.kgc.scd.bean.ShopUser;
import com.kgc.scd.constant.CommonConstant;
import com.kgc.scd.service.ShopHistoryFeignService;
import com.kgc.scd.util.PageSupport;
import com.kgc.scd.util.RequestResult;
import com.kgc.scd.util.ResultBuildUtil;
import com.kgc.scd.vo.ShopGoodsVO;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.jni.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * Created On : 2022/11/14 0014.
 * <p>
 * Author : shiguangchuan
 * <p>
 * Description: 历史足迹前端操作入口
 */
@Slf4j
@RestController
@RequestMapping("/sgc")
@Api(tags = "历史足迹接口",value = "通过浏览的商品详情id，记录用户的历史足迹，或查询历史足迹")
public class ShopHistoryController {

    @Autowired
    private ShopHistoryFeignService shopHistoryFeignService;

    /**
     * @author : shiguangchuan
     * @date   : 2022/11/14 0014
     * @param  : [goodsId, userId]
     * @return : void
     * @description : 通过用户id及商品id更新足迹信息
     */
    @PostMapping("/updateHistory")
    @ApiOperation(value = "更新足迹信息",notes = "通过传入商品id及用户id，将足迹保存或更新")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId",value = "商品id",required = true)
    })
    @TokenPermission
    public void updateHistory(@RequestParam String goodsId,
                              @ApiIgnore @RequestShopUser ShopUser shopUser){

        if (goodsId == null){
            return;
        }

        // 调用业务接口实现更新足迹信息
        shopHistoryFeignService.feignInvokeUpdateHistory(goodsId,shopUser.getId());

    }

    /**
     * @author : shiguangchuan
     * @date   : 2022/11/15 0015
     * @param  : [pageNo, pageSize, shopUser]
     * @return : com.kgc.scd.util.RequestResult<com.kgc.scd.util.PageSupport<com.kgc.scd.vo.ShopGoodsVO>>
     * @description : 显示用户足迹信息，可带条件查询
     */
    @GetMapping("/selectHistory")
    @ApiOperation(value = "首次进入足迹页面",notes = "通过用户，将用户的足迹信息全部展示，带分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo",value = "页码"),
            @ApiImplicitParam(name = "pageSize",value = "页面条数")
    })
    @TokenPermission
    public RequestResult<PageSupport<ShopGoodsVO>> selectHistoryList(@RequestParam(defaultValue = CommonConstant.DEFAULT_INIT_PAGE_NO) Integer pageNo,
                                              @RequestParam(defaultValue = CommonConstant.DEFAULT_INIT_PAGE_SIZE) Integer pageSize,
                                              @ApiIgnore @RequestShopUser ShopUser shopUser){

        PageSupport<ShopGoodsVO>  support = shopHistoryFeignService.feignInvokeSelectHistory(pageNo,pageSize, shopUser.getId());

        if (support.getTotalCount() != 0) {
            // 足迹中存在信息
            return ResultBuildUtil.success(support);
        }

        return ResultBuildUtil.fail("300","当前还没有浏览过任何商品。");

    }


    /**
     * @author : shiguangchuan
     * @date   : 2022/11/15 0015
     * @param  : [ids]
     * @return : com.kgc.scd.util.RequestResult<java.lang.String>
     * @description : 删除单个或多个足迹信息 
     */
    @PostMapping("/deleteHistory")
    @ApiOperation(value = "单个或批量删除足迹信息",notes = "用户通过id删除单个或多个足迹信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids",value = "足迹id列表",required = true,allowMultiple = true)
    })
    public RequestResult<String> deleteHistoryById(@RequestParam List<String> ids){
        
        // 调用远程删除足迹的接口
        if(shopHistoryFeignService.feignInvokeDeleteHistoryById(ids)){
            return ResultBuildUtil.success("删除足迹信息成功");
        }
        
        return ResultBuildUtil.fail("删除足迹信息失败");
        
    }

    /**
     * @author : shiguangchuan
     * @date   : 2022/11/15 0015
     * @param  : [shopUser]
     * @return : com.kgc.scd.util.RequestResult<java.lang.String>
     * @description : 通过用户id执行足迹信息的全部删除操作
     */
    @PostMapping("/deleteAllHistory")
    @ApiOperation(value = "删除全部足迹信息",notes = "用户执行删除全部足迹信息的操作")
    public RequestResult<String> deleteAllHistoryByUserId(@ApiIgnore @RequestShopUser ShopUser shopUser){

        // 调用远程全部删除足迹信息的接口
        if (shopHistoryFeignService.feignInvokeDeleteHistoryByUserId(shopUser.getId())){
            return ResultBuildUtil.success("足迹已全部删除!");
        }

        return ResultBuildUtil.fail("你还没有浏览过任何商品，请先去浏览哦！");

    }
}
